package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.FileParser;
import model.Manuscript;
import model.Recommendation;
import model.Review;
import model.State;
import model.Status;
import model.User;

/**
 * Class that is the JFramefor the add recommendation functionality.
 * @author Kevin Anderson
 * @version Autumn 2012
 */
@SuppressWarnings("serial")
public class AddRecommendationFrame extends JFrame {

	/**
	 * Field to hold the Manuscript that will be recommended.
	 */
	private Manuscript my_manuscript;
	
	/**
	 * Field to hold the current state of the system.
	 */
	private State my_state;
	
	/**
	 * Field to hold the JTextField that contains the file path of the recommendation.
	 */
	private JTextField my_file_path;
	
	/**
	 * Field to hold the pull down menu that states whether the manuscript has been
	 * recommended or not.
	 */
	private JComboBox<String> my_reco_box;

	/**
	 * Constructor for an add recommendation frame.
	 * @param manuscript the manuscript to be recommended.
	 * @param the_state the current state of the system.
	 */
	public AddRecommendationFrame(Manuscript manuscript, State the_state) {

		super("Upload Recommendation");

		my_manuscript = manuscript;
		my_state = the_state;
		setResizable(true); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * Method to set up the JFrame.
	 */
	public void setup() {
		add(new AddReccPanel());
		pack();
	}


	/**
	 * Class to create the panel for the add rec functionality.
	 * @author Kevin Anderson
	 * @version Autumn 2012
	 */
	@SuppressWarnings("serial")
	private class AddReccPanel extends JPanel
	{

		/**
		 * Constructor for the Add rec panel.
		 */
		public AddReccPanel() {
			super();
			setup();
		}

		/**
		 * Method to setup the Add rec panel.
		 */
		public void setup() {
			GridLayout exp_layout = new GridLayout(0,2);
			this.setLayout(exp_layout);

			add(new JLabel("Choose a Recommendation: "));

			my_reco_box = new JComboBox<String>();
			my_reco_box.addItem(Recommendation.RECOMMENDED);
			my_reco_box.addItem(Recommendation.NOT_RECOMMENDED);
			add(my_reco_box);

			my_file_path = new JTextField();
			add(my_file_path);
			JFileChooser fc = new JFileChooser();
			BrowseAction browse = new BrowseAction(AddRecommendationFrame.this, fc, my_file_path);
			add(new JButton(browse));
			add(new JButton(new CloseAction(AddRecommendationFrame.this)));
			add(new JButton(new UploadAction()));

		}

		/**
		 * Method to verify that the required fields are filled in.
		 * @return valid if the required fields are filled in.
		 */
		private boolean verify()
		{
			boolean valid = (my_file_path.getText().trim().length() > 0);
			if (!valid)
				JOptionPane.showMessageDialog(this, "Please select a file!");

			return valid;

		}

		/**
		 * Class to upload a recommendation.
		 * @author Kevin Anderson
		 * @version 12/5/2012
		 */
		private class UploadAction extends AbstractAction
		{

			/**
			 * Constructor for an upload action.
			 */
			public UploadAction() {
				super("Upload");
				putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
				putValue(Action.SELECTED_KEY, true);
			}

			/**
			 * Method to upload the recommendation.
			 * @param arg0 the action event that triggers the method.
			 */
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (verify())
				{
					Recommendation reco = new Recommendation(FileParser.NEW_ELEMENT, my_manuscript.getMy_id(), 
							my_state.getUser().getID(), (String) my_reco_box.getSelectedItem(),
							my_file_path.getText());
					if(my_state.getParser().writeRecommendation(reco))
					{
						my_manuscript.setMy_status(Status.RECOMMENDED);
						my_state.getParser().writeManuscript(my_manuscript);
						JOptionPane.showMessageDialog(AddRecommendationFrame.this, "You Have successfully Uploaded your recommendation.");
						AddRecommendationFrame.this.dispose();
					} else {
						JOptionPane.showMessageDialog(AddRecommendationFrame.this, "Couldn't upload file.  Try again.");	
					}
				}
			}
		}//End class upload action.
	}//End class Add rec panel.
} //End class Add Recommendation Frame.